package top.zsyle.org.controller;

import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.zsyle.basic.util.AjaxResult;
import top.zsyle.basic.util.PageList;
import top.zsyle.org.domain.Employee;
import top.zsyle.org.query.EmployeeQuery;
import top.zsyle.org.service.IEmployeeService;

import java.util.List;

@RestController()
@RequestMapping("/emp")
@Api(value = "员工的API",description="员工相关的CRUD功能")
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeService;



    /**
     * 查询全部
     * @return
     */
    @GetMapping()
    public List<Employee> findAll(){
        return employeeService.findAll();
    }

    /**
     * 查找同一店铺的员工和店长
     * @param shopId
     * @return
     */
    @GetMapping("/list/{shopId}")
    public List<Employee> findAllByShopId(@PathVariable("shopId") Long shopId){
        return employeeService.findAllByShopId(shopId);
    }

    /**
     * 新增、修改
     * @param employee
     * @return
     */
    @PutMapping()
    public AjaxResult addOrUpdate(@RequestBody Employee employee){
        if (null != employee.getId()){
            //修改
            employeeService.update(employee);
        }else {
            employeeService.add(employee);
        }
        return AjaxResult.me().success();
    }

    /**
     * 分页查询
     * @param query
     * @return
     */
    @PostMapping()
    public PageList<Employee> queryByPage(@RequestBody EmployeeQuery query){
        return employeeService.queryByPage(query);
    }

    /**
     * 通过id删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        employeeService.delete(id);
        return AjaxResult.me().success();
    }

    @PatchMapping()
    public AjaxResult batchDelete(@RequestBody List<Long> ids){
        employeeService.batchDelete(ids);
        return AjaxResult.me().success();
    }
    /**
     * 通过 id 查询一个
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Employee findById(@PathVariable("id") Long id){
        return employeeService.findById(id);
    }

    /**
     * 根据部门id查询当前部门下的全部员工信息
     * @param id
     * @return
     */
    @GetMapping("/dept/{id}")
    public List<Employee> findAllByDeptId(@PathVariable("id") Long id){
        return employeeService.findAllByDeptId(id);
    }
}
